﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using EPiServer;
using EPiServer.Core;
using EPiServer.Security;
using EPiServer.Web.Mvc;

namespace EPiBoost.Core.Base.Controllers
{
	public class CoreBasePageController<TP> : PageController<TP> where TP : PageData
	{
		protected TP CurrentPage
		{
			get
			{
				//returns the current page as T using a PageRouteHelper
				//As mentioned by Fredrik Vig in the comments:
				return (TP)PageContext.Page;
			}
		}

		protected override void OnAuthorization(AuthorizationContext filterContext)
		{
			CheckAccess();
			base.OnAuthorization(filterContext);
		}

		protected void CheckAccess()
		{
			if (CurrentPage.QueryAccess().HasFlag(AccessLevel.Read))
				return;
			ServeAccessDenied();
		}

		protected virtual void ServeAccessDenied()
		{
			AccessDeniedDelegate accessDenied = DefaultAccessDeniedHandler.CreateAccessDeniedDelegate();
			accessDenied(CurrentPage);
		}


	}
}
